package com.zhangsl.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhangsl.service.UserService;
import com.zhangsl.utils.MD5;
import com.zhangsl.vo.UserVO;

@Controller
@RequestMapping(path="/user")
public class UserController {
	
	private final Log log=LogFactory.getLog(this.getClass());
	@Autowired
	private UserService userService;
	@RequestMapping(path= {"/login"},method= {RequestMethod.POST})
	public @ResponseBody  String login(UserVO user,HttpSession httpSession) {
		log.info("【user】"+user.toString());
		UserVO userVO =userService.findUserByName(user.getPwd());
		if(null != userVO &&  userVO.getPwd().equals(MD5.getMD5ofStr(user.getPwd()))) {
			String currentTime = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss").format(new Date());
			log.info("currentTime:"+currentTime);
			userVO.setLastLoginTime(currentTime);
			httpSession.setAttribute("user", userVO);
//			httpSession.setAttribute("loginTime", currentTime);
			userService.updateUserLoginTime(userVO);
			return "{'msg':'Hello-Word'}";
		}else {
			return "{'msg':'False'}";
		}
	}
}
